package cn.nexd.app.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;

import cn.nexd.app.activity.inter.ISignInActivityView;
import cn.nexd.app.arsenal.R;
import cn.nexd.app.presenter.ISignInPresenterImpl;
import cn.nexd.app.presenter.inter.ISignInPresenter;
import cn.nexd.app.util.SharedPreferencesUtil;
import cn.nexd.app.widget.AutoEmailEditText;

/**
 * Sign in
 * <p/>
 * compare with location file
 * <p/>
 * A login screen that offers login via email/password.
 * <p/>
 * create by wangxu
 */
public class SignInActivity extends AppCompatActivity implements View.OnClickListener, ISignInActivityView {

    private AutoEmailEditText etEmail;
    private EditText etPassword;
    private CheckBox ckRememberEmail;
    private ISignInPresenter iSignInPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AnalyticsConfig.enableEncrypt(true);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
        }
        iSignInPresenter = new ISignInPresenterImpl(this, this);
        etEmail = (AutoEmailEditText) findViewById(R.id.email);
        etPassword = (EditText) findViewById(R.id.password);
        ckRememberEmail = (CheckBox) findViewById(R.id.remember_password_check_box);
        Button btLogin = (Button) findViewById(R.id.email_sign_in_button);
        ckRememberEmail.setOnClickListener(this);
        // 登录按钮点击事件监听
        btLogin.setOnClickListener(this);
        if (!TextUtils.isEmpty(SharedPreferencesUtil.getAccount(this))) {
            etEmail.setText(SharedPreferencesUtil.getAccount(this));
            etPassword.requestFocus();
            ckRememberEmail.setChecked(true);
        }


    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        etEmail.dismissDropDown();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        etEmail.dismissDropDown();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        etEmail.dismissDropDown();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.email_sign_in_button:

                if (iSignInPresenter.checkUser(etEmail.getText().toString().trim(), etPassword.getText().toString().trim())) {
                    iSignInPresenter.signIn(etEmail.getText().toString().trim(),
                            etPassword.getText().toString().trim(),
                            ckRememberEmail.isChecked());
                }
                break;
        }
    }


    @Override
    public void setViewError(String errorMsg, int which) {
        switch (which) {
            case R.id.email:
                etEmail.setError(errorMsg);
                etEmail.requestFocus();
                break;

            case R.id.password:
                etPassword.setError(errorMsg);
                etPassword.requestFocus();
                break;
        }
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}

